﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lync.Widget.Library.Presentation.Entities;
using Lync.Widget.Library.Presentation.Models;
using Lync.Widget.Library.Presentation.Presenters;

namespace Lync.Widget.Library.Presentation.Views
{
    public class FiltersView : UserControl, IFiltersView
    {

        #region Controls

        protected PlaceHolder FiltersResultPlaceHolder;
        protected Repeater FilterRepeater;
        protected Label ErrorMesssageLabel;
        protected Button SaveButton;
        protected PlaceHolder SavedPlaceHolder;
        protected Label SavedLabel;
        protected Button DownloadButton;

        protected TextBox FilterIdTextBox;
        protected TextBox FilterNameTextBox;
        protected TextBox FilterQuerystringParamTextBox;
        protected TextBox FilterWildcardTextBox;


        #endregion

        #region IView Properties


        public IFilter NewFilter
        {
            get
            {
                return new Filter
                {
                    Id = FilterIdTextBox.Text,
                    Name = FilterNameTextBox.Text,
                    QuerystringParam = FilterQuerystringParamTextBox.Text,
                    Wildcard = FilterWildcardTextBox.Text,
                };
            }
        }
        public bool Saved
        {
            set
            {
                SavedPlaceHolder.Visible = value;                                
                ErrorMessage = null;
            }
        }
        public bool ShowDownloadButton
        {
            set { DownloadButton.Visible = value; }
        }

        public void AddToList(IFilter filter)
        {
            if (filter != null)
            {
                var filters = Filters;
                filters.Add(filter);
                Filters = filters;    
            }            
        }
        public void ResetNewFilter()
        {
            FilterIdTextBox.Text = null;
            FilterNameTextBox.Text = null;
            FilterQuerystringParamTextBox.Text = null;
            FilterWildcardTextBox.Text = null;
        }

        public IList<IFilter> Filters
        {
            get
            {
                var temp = new List<IFilter>();
                FillFilters(temp);
                return temp;
            }
            set
            {
                FilterRepeater.DataSource = value;
                FilterRepeater.DataBind();
            }
        }
        public bool ShowResults
        {
            set { FiltersResultPlaceHolder.Visible = value; }
        }
        public bool ShowSaveButton
        {
            set { SaveButton.Visible = value; }
        }
        public string ErrorMessage
        {
            get { return ErrorMesssageLabel.Text; }
            set
            {
                ErrorMesssageLabel.Text = value;
                ErrorMesssageLabel.Visible = !String.IsNullOrEmpty(value);
            }
        }

        #endregion

        
        
        
        private IFiltersPresenter m_Presenter;

        protected override void OnInit(System.EventArgs e)
        {
            m_Presenter = new FiltersPresenter(this, new FiltersModel());
        }

        protected override void OnLoad(System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                m_Presenter.OnLoad();
            }
        }
        
        protected void SaveButton_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (m_Presenter.Save())
                {
                    m_Presenter.UpdateView();                    
                }
            }            
        }

        protected void DownloadButton_OnClick(object sender, EventArgs e)
        {
            m_Presenter.Download(HttpContext.Current);
        }


        private void FillFilters(List<IFilter> filters)
        {
            var items = (from item in FilterRepeater.Items.Cast<RepeaterItem>().ToList()                         
                         select new Filter
                         {                             
                             Id = ((TextBox)item.FindControl("FilterIdTextBox")).Text,
                             Name = ((TextBox)item.FindControl("FilterNameTextBox")).Text,
                             QuerystringParam = ((TextBox)item.FindControl("FilterQuerystringParamTextBox")).Text,
                             Wildcard = ((TextBox)item.FindControl("FilterWildcardTextBox")).Text,                             
                         });


            filters.AddRange(items);
        }

        protected void AddFilterButton_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (m_Presenter.AddFilter())
                {
                    m_Presenter.UpdateView();
                    m_Presenter.ResetNewFilter();
                }                
            }            
        }

        protected void RemoveButton_OnClick(object sender, EventArgs e)
        {
            var button = (Button) sender;
            var id = button.CommandArgument;
            
            if (m_Presenter.RemoveFilter(id))
            {
                m_Presenter.UpdateView();    
            }
        }
    }
}